"use strict";

module.exports = function (sequelize, DataTypes) {
    var User = sequelize.define("user", {
        phone: {
            type: DataTypes.STRING,
            allowNull: true,
            unique: true,
            validate: {
                is: /^1[3|4|5|7|8][0-9]\d{8}$/,
            },
        },
        email: {
            type: DataTypes.STRING,
            allowNull: true,
            validate: {
                isEmail: true,
            },
        },
        description: {
            type: DataTypes.TEXT,
        },
        nickname: {
            type: DataTypes.STRING(64),
            allowNull: false,
            unique: false,
        },
        avatar: {
            type: DataTypes.STRING(200),
            allowNull: true
        },
        sex: {
            type: DataTypes.ENUM,
            values: ['male', 'female', 'unset'],
            defaultValue: 'unset',
        },
        freeze: {
            type: DataTypes.BOOLEAN,
            defaultValue: false,
        },
        birthday: {
            type: DataTypes.DATE,
            allowNull: true
        },
        latitude: {
            type: DataTypes.DOUBLE,
            allowNull: true,
            defaultValue: null,
            validate: { min: -90, max: 90 }
        },
        longitude: {
            type: DataTypes.DOUBLE,
            allowNull: true,
            defaultValue: null,
            validate: { min: -180, max: 180 }
        },
        verifyBin: {
            type: DataTypes.TEXT,
            allowNull: true,
        },
        wxOpenId: {
            type: DataTypes.STRING(64),
            allowNull: true,
        },
        wxAccessToken: {
            type: DataTypes.STRING(64),
            allowNull: true
        },

        //dca_user_extra
        hasJIMRegistered: {
            type: DataTypes.BOOLEAN,
            defaultValue: false,
        },
        statusDesc: {
            type: DataTypes.TEXT,
        },
        allowFind: {
            type: DataTypes.BOOLEAN,
            defaultValue: true,
        },
        level: {
            type: DataTypes.INTEGER,
        },
        virtualCoin: {
            type: DataTypes.INTEGER,
        },
        rating: {
            type: DataTypes.INTEGER,
        },
        activeValue: {
            type: DataTypes.INTEGER,
        },
        isLTSOnline: {
            type: DataTypes.BOOLEAN,
        },
        lastCheckLTSOnlneTime: {
            type: DataTypes.DATE,
        }
    },
        {
            freezeTableName: true,
            paranoid: true,
            indexes: [
                {
                    unique: true,
                    fields: ['phone']
                },
                {
                    unique: true,
                    fields: ['email']
                },
                {
                    unique: false,
                    fields: ['nickname']
                }
            ],
        });

    User.associate = function (models) {
        // Using additional options like CASCADE etc for demonstration
        // Can also simply do Task.belongsTo(models.User);
        // Task.belongsTo(models.User, {
        //     onDelete: "CASCADE",
        //     foreignKey: {
        //         allowNull: false
        //     }
        // });
        
        var DcaUserRoutingHistory = models.dcaUserRoutingHistory;
        User.hasMany(DcaUserRoutingHistory);
        DcaUserRoutingHistory.belongsTo(User);

        var LoginSession = models.loginSession; 
        User.hasMany(LoginSession);
        LoginSession.belongsTo(User);

        var ConversationHistory = models.conversationHistory;

        var ChatMessage = models.chatMessage;
        ConversationHistory.hasMany(ChatMessage);
        ChatMessage.belongsTo(ConversationHistory);
        User.hasMany(ChatMessage);
        ChatMessage.belongsTo(User);
    }

    return User;
};